package com.grocery.controller;

import com.grocery.service.impl.Loginservice;
import com.grocery.bean.User;
import com.grocery.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    Loginservice loginservice;

    @RequestMapping("page/dologin")
    @ResponseBody
    public String userLogin(User user, HttpSession session){
        //执行登录业务
        User u=loginservice.dologin(user);
        if(u!=null&&u.getType()==0){
            //记录登录状态
            session.setAttribute(SYS.USER_LOGIN,u);
            return "success";
        }else if(u!=null&&u.getType()==1){
            return "adminsuccess";
        }
        else {
            return "fail";
        }
    }

    @RequestMapping("page/getuser")
    @ResponseBody
    public User getUserByID(@SessionAttribute("USER_LOGIN") User user){
        return loginservice.getUserByID(user.getId());
    }


//    @RequestMapping("page/dologin")
    public String systemLogin(){
        return "success";
    }
}
